import datetime
import os.path
import sys
import os
from subprocess import check_output

from selenium import webdriver
# from selenium.webdriver.edge.service import Service
from selenium.webdriver.chrome.service import Service
from webdriver_manager.microsoft import EdgeChromiumDriverManager
# from selenium.webdriver.edge.options import Options
from selenium.webdriver.chrome.options import Options
import logging

# 使用 Chrome 驱动路径
CHROME_DRIVER_PATH = "D:\\chromedriver-win64\\chromedriver-win64\\chromedriver.exe"
#r"D:\\edgedriver_win64\\msedgedriver.exe"
# 启用详细日志 - 在文件开头或类初始化中
logging.basicConfig(level=logging.INFO)

#创建一个浏览器对象a
class Driver:
    driver=""
    def __init__(self):
        options=Options()
        options.add_experimental_option("excludeSwitches",["enable-automation"])
        options.add_experimental_option('useAutomationExtension', False)

        # 其他优化选项
        options.add_argument("--disable-blink-features=AutomationControlled")
        options.add_argument("--disable-extensions")
        options.add_argument("--no-sandbox")
        options.add_argument("--disable-dev-shm-usage")
        options.add_argument("--remote-debugging-port=0")
        # options.binary_location = r"D:\\chromedriver-win64\\chrome-win64\\chrome.exe"
        #self.driver=webdriver.Edge(service=Service(CHROME_DRIVER_PATH),options=options)
        self.driver = webdriver.Chrome(service=Service(CHROME_DRIVER_PATH), options=options)
        #隐式等待
        self.driver.implicitly_wait(1)
        print("浏览器启动成功！")
    def getScreeShot(self):
        #创建屏幕截屏
        #创建文件夹
        dirname=datetime.datetime.now().strftime("%Y-%m-%d")
        #判读dirname是否存在，不存在就创建文件夹
        if not os.path.exists("../images/"+dirname):
            os.mkdir("../images/"+dirname)

        filename=sys._getframe().f_back.f_code.co_name+"-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
        self.driver.save_screenshot("../images/"+dirname+"/"+filename)


#单例模式
BlogDriver=Driver()